package com.kaikeba.wx.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseBody(value = "/wx/listExpressByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp){
        String phone = null;
        Expressman wxMan = null;
        User wxUser = UserUtil.getWxUser(req.getSession());
        if(wxUser == null){
            wxMan = UserUtil.getWxExpressman(req.getSession());
            phone = wxMan.getPhone();
        }else{
            phone = wxUser.getPhone();
        }
        List<Express> expresses = ExpressService.findByUserPhone(phone);
        List<BootstrapTableExpress> BTExpresses = expresses.stream().map(e -> {
            BootstrapTableExpress bte = new BootstrapTableExpress();
            bte.setId(e.getId());
            bte.setNumber(e.getNumber());
            bte.setUsername(e.getUsername());
            bte.setUserPhone(e.getUserPhone());
            bte.setCompany(e.getCompany());
            bte.setCode(e.getCode());
            bte.setInTime(DateFormatUtil.format(e.getInTime()));
            bte.setOutTime(e.getOutTime()==null?"未取件":DateFormatUtil.format(e.getOutTime()));
            bte.setStatus(e.getStatus()==0?"未取件":"已取件");
            bte.setSysPhone(e.getSysPhone());
            return bte;
        }).collect(Collectors.toList());
        Message msg = new Message();
        if(BTExpresses == null || BTExpresses.size() == 0){
            msg.setStatus(-1);
        }else{
            Stream<BootstrapTableExpress> status0Expresses = BTExpresses.stream().filter((express -> {
                if("未取件".equals(express.getStatus())){
                    return true;
                }else{
                    return false;
                }
            })).sorted((e1, e2)->{
                long time1 = DateFormatUtil.parse(e1.getInTime()).getTime();
                long time2 = DateFormatUtil.parse(e2.getInTime()).getTime();
                return (int)(time1-time2);
            });
            Stream<BootstrapTableExpress> status1Expresses = BTExpresses.stream().filter((express) -> {
                if ("已取件".equals(express.getStatus())) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((e1, e2)->{
                long time1 = DateFormatUtil.parse(e1.getInTime()).getTime();
                long time2 = DateFormatUtil.parse(e2.getInTime()).getTime();
                return (int)(time1-time2);
            });
            List<BootstrapTableExpress> s0ExpressList = status0Expresses.collect(Collectors.toList());
            List<BootstrapTableExpress> s1ExpressList = status1Expresses.collect(Collectors.toList());

            Map<String, List<BootstrapTableExpress>> map = new HashMap<>();
            map.put("s0Expresses", s0ExpressList);
            map.put("s1Expresses", s1ExpressList);
            msg.setStatus(1);
            msg.setObject(map);
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/listUserExpress.do")
    public String listExpressByQrPhoneAndStatus(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        int status = Integer.parseInt(req.getParameter("status"));
        List<BootstrapTableExpress> expresses = ExpressService.findByUserPhone(phone).stream().filter(express -> {
            if (express.getStatus() == status) {
                return true;
            } else {
                return false;
            }
        }).map(express -> {
            BootstrapTableExpress bte = new BootstrapTableExpress();
            bte.setId(express.getId());
            bte.setNumber(express.getNumber());
            bte.setUsername(express.getUsername());
            bte.setUserPhone(express.getUserPhone());
            bte.setCompany(express.getCompany());
            bte.setCode(express.getCode());
            bte.setInTime(DateFormatUtil.format(express.getInTime()));
            bte.setOutTime(express.getOutTime()==null?"未取件":DateFormatUtil.format(express.getOutTime()));
            bte.setStatus(express.getStatus()==0?"未取件":"已取件");
            bte.setSysPhone(express.getSysPhone());
            return bte;
        }).collect(Collectors.toList());
        Message msg = new Message();
        if(expresses == null || expresses.size() == 0){
            msg.setStatus(-1);
            msg.setResult("该用户没有快递");
        }else{
            msg.setStatus(1);
            msg.setObject(expresses);
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/wx/getAnExpressByQRCode.do")
    public String getExpress(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        Express express = ExpressService.findByCode(code);

        Message msg = new Message();
        if(express == null){
            msg.setStatus(-1);
            msg.setResult("未找到对应快递");
        }else{
            BootstrapTableExpress bte = new BootstrapTableExpress();
            bte.setId(express.getId());
            bte.setNumber(express.getNumber());
            bte.setUsername(express.getUsername());
            bte.setUserPhone(express.getUserPhone());
            bte.setCompany(express.getCompany());
            bte.setCode(express.getCode());
            bte.setInTime(DateFormatUtil.format(express.getInTime()));
            bte.setOutTime(express.getOutTime()==null?"未取件":DateFormatUtil.format(express.getOutTime()));
            bte.setStatus(express.getStatus()==0?"未取件":"已取件");
            bte.setSysPhone(express.getSysPhone());
            msg.setStatus(1);
            msg.setObject(bte);
        }
        return JSONUtil.toJson(msg);
    }
}
